package com.anysoft.web;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebView;

import com.anysoft.R;
import com.anysoft.WebViewNetworkProtocol;
import com.anysoft.event.TimeOutEvent;
import com.anysoft.linstener.impl.OnShareEventLinstenerImpl;
import com.anysoft.me.base.BaseWebViewActivity;
import com.anysoft.utils.ActivityUtils;
import com.anysoft.utils.AnalyticsEventUtils;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.HttpUtils;
import com.anysoft.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.OnClick;

import static com.anysoft.HostConstant.HOST_WEB_URL_HEADER;

/**
 * 公告详情页
 * Created by wyb on 2017/3/26.
 */

public class AnnouncemetsDetailActivity extends BaseWebViewActivity {

    private String sTitle;
    private String sId;
    private String sUrl;
    private String shareUrl = "";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShowTitleBar(true);
//        SharePopup.setOnShareEventLinstener(new OnShareEventLinstenerImpl() {
//            @Override
//            public void onShareSuccess() {
//                AnalyticsEventUtils.toShareNoticeEvent(AnnouncemetsDetailActivity.this);
//            }
//        });
        title.setText("公告");
        shareButtonTitle1.setVisibility(View.VISIBLE);
        getIntentValue();
        if (StringUtils.isNullOrEmpty(sUrl)) {
            shareUrl = HOST_WEB_URL_HEADER + "broadcastDetail.html?isShare=1&id=" + sId;
            generateUrl("broadcastDetail", HttpUtils.getCommonParams2StringByWeb(this) + "&id=" + sId);
        } else {
            loadUrl = sUrl;
        }
        if (CommonUtil.isNetWorkConnected(this)) {
            setWebViewConfig(loadUrl);
        }
    }

    private void getIntentValue() {
        if (getIntent() == null) {
            finish();
        }
        if (getIntent().hasExtra("id")) {
            sId = getIntent().getStringExtra("id");
        }
        if (getIntent().hasExtra("title")) {
            sTitle = getIntent().getStringExtra("title");
        }
        if (getIntent().hasExtra("url")) {
            sUrl = getIntent().getStringExtra("url");
        }
        if (!StringUtils.isNullOrEmpty(sTitle)) {
            title.setText("" + sTitle);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @Override
    public void onshouldOverrideUrlLoadingCallBack(WebView webview, String url) {
        if (url.contains("prompt.html")) {
            mIntent.setClass(mActivity, SelectWarmPromptActivity.class);
            mIntent.putExtra("url", url);
            startActivity(mIntent);
        } else if (url.contains(WebViewNetworkProtocol.GOTO_LOGIN)) {
            ActivityUtils.gotoLogin(mActivity, hasUser(), false, findUserId());
            EventBus.getDefault().post(new TimeOutEvent(true));
        } else if (url.contains(WebViewNetworkProtocol.GO_QUESTION)) {
            startRiskActivity(null);
        } else {
            webview.loadUrl(url);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeOutCallBack(TimeOutEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

    @OnClick({R.id.shareButtonTitle1})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shareButtonTitle1:
                //TODO 需要H5对应连接地址
//                SharePopup.Share(mActivity, "" + sTitle, shareUrl, "点击查看详情");
                break;
            default:
                super.onClick();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
